package com.h3c.nem.controller;

import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.PageInfo;
import com.h3c.nem.entity.TbOfferSendRecord;
import com.h3c.nem.service.ISapSyncService;
import com.h3c.nem.service.ITbOfferSendRecordService;
import com.h3c.nem.vo.OfferSendRecordVO;
import com.sap.conn.jco.JCoStructure;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Objects;

/**
 * offer发放记录(TbOfferSendRecord)表控制层
 *
 * @author makejava
 * @since 2022-06-08 09:15:26
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tbOfferSendRecord")
public class TbOfferSendRecordController {
    /**
     * 服务对象
     */
    private final ITbOfferSendRecordService ITbOfferSendRecordService;

    private final ISapSyncService sapSyncService;

    @GetMapping("/getOfferSendRecord")
    public Result<PageInfo<OfferSendRecordVO>> getOfferSendRecord(@RequestParam(value = "workerIds", defaultValue = "") String workerIds,
                                                                  @RequestParam(value = "startDate", defaultValue = "") String startDate,
                                                                  @RequestParam(value = "endDate", defaultValue = "") String endDate,
                                                                  @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        PageInfo<OfferSendRecordVO> res = ITbOfferSendRecordService.getOfferSendRecord(currentPage, pageSize, workerIds,
                startDate, endDate);
        return new Result<>(true, StatusCodeEnum.NORMAL, res);
    }

    /**
     * 获取发送的邮件内容
     *
     * @return
     */
    @GetMapping("/previewSendContent")
    public Result<String> previewSendContent(@RequestParam String recordId) {
        TbOfferSendRecord sendRecord = ITbOfferSendRecordService.getById(recordId);
        String sendTemplateContent = sendRecord.getSendTemplateContent();
        JCoStructure returnStructure = sapSyncService.linkBpmGetData(sendRecord.getWorkerId());
        //预备员工工资
        BigDecimal Betrg = returnStructure.getBigDecimal("BETRG");
        //月标准工资
        BigDecimal Z1jbx = returnStructure.getBigDecimal("Z1JBX");
        //年标准工资
        BigDecimal Z0020 = returnStructure.getBigDecimal("Z0020");
        //年目标浮动奖金
        BigDecimal Z0040 = returnStructure.getBigDecimal("Z0040");
        //目标浮动奖金比例
        BigDecimal Z0030 = returnStructure.getBigDecimal("Z0030");
        //目标总现金包
        BigDecimal Znmbzje = returnStructure.getBigDecimal("ZNMBZJE");
        HashMap<String, String> salaryParams = new HashMap<>(6);
        salaryParams.put("Betrg", Objects.nonNull(Betrg) ? Betrg.toString() : "");
        salaryParams.put("Z1jbx", Objects.nonNull(Betrg) ? Z1jbx.toString() : "");
        salaryParams.put("Z0020", Objects.nonNull(Betrg) ? Z0020.toString() : "");
        salaryParams.put("Z0040", Objects.nonNull(Betrg) ? Z0040.toString() : "");
        salaryParams.put("Z0030", Objects.nonNull(Betrg) ? Z0030.toString() : "");
        salaryParams.put("Znmbzje", Objects.nonNull(Betrg) ? Znmbzje.toString() : "");
        //补充薪资内容
        for (String salaryKey : salaryParams.keySet()) {
            sendTemplateContent = sendTemplateContent.replaceAll("\\$\\{" + salaryKey + "\\}", salaryParams.get(salaryKey));
        }
        return new Result<>(true, StatusCodeEnum.NORMAL, sendTemplateContent);
    }

}

